package com.noahplat.device.controller;

import com.noahplat.common.utils.Result;
import com.noahplat.device.pojo.Device;
import com.noahplat.device.service.DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.io.File;
import java.io.IOException;

/**
 * @ClassName: DeviceController
 * @Description: 设备controller
 * @Author 李景星
 * @Date 2021/3/9
 * @Version 1.0
 */
@RestController
@RequestMapping("/api/device")
@Validated
public class DeviceController {

    @Autowired
    private DeviceService deviceService;

    /**
     * 获取设备详细信息
     * @param imei
     * @return
     */
    @PreAuthorize("hasAuthority('noahplat-service-device:DeviceController:getDevice')")
    @GetMapping("/getDevice")
    public Result<Device> getDevice(@NotEmpty(message = "imei不能为空") String imei){
        return deviceService.getDevice(imei);
    }

    /**
     * 条件查询设备信息
     * @param page
     * @param limit
     * @param dealer
     * @param imei
     * @return
     */
    @PreAuthorize("hasAuthority('noahplat-service-device:DeviceController:getDevices')")
    @GetMapping("/getDevices")
    public Result getDevices(@RequestParam(defaultValue = "1")Integer page, @RequestParam(defaultValue = "10")Integer limit, @NotNull(message = "账套ID不能为空 ") @Min(value = 1, message = "账套ID不合法")Long dealer, String imei){
        return deviceService.getDevices(page, limit, dealer, imei);
    }

    /**
     * 查找定位设备
     * @param page
     * @param limit
     * @param dealer
     * @param imei
     * @return
     */
    @PreAuthorize("hasAuthority('noahplat-service-device:DeviceController:getSiteDevices')")
    @GetMapping("/getSiteDevices")
    public Result getSiteDevices(@RequestParam(defaultValue = "1")Integer page,@RequestParam(defaultValue = "10")Integer limit,@NotNull(message = "账套ID不能为空 ") @Min(value = 1, message = "账套ID不合法")Long dealer,String imei){
        return deviceService.getSiteDevices(page, limit, dealer, imei);
    }

    /**
     * 根据ID查询设备信息
     * @param id
     * @return
     */
    @PreAuthorize("hasAuthority('noahplat-service-device:DeviceController:getDeviceAllInfoById')")
    @GetMapping("/getDeviceAllInfoById")
    public Result getDeviceAllInfoById(@NotNull(message = "ID不能为空 ") @Min(value = 1, message = "ID不合法")Long id){
        return deviceService.getDeviceAllInfoById(id);
    }

    /**
     * 查询设备数量
     * @param dealer
     * @return
     */
    @PreAuthorize("hasAuthority('noahplat-service-device:DeviceController:getDeviceCount')")
    @GetMapping("/getDeviceCount")
    public Result getDeviceCount(@NotNull(message = "账套ID不能为空 ") @Min(value = 1, message = "账套ID不合法")Long dealer){
        return deviceService.getDeviceCount(dealer);
    }

    /**
     * 分页查询设备数量
     * @param page
     * @param limit
     * @return
     */
    @PreAuthorize("hasAuthority('noahplat-service-device:DeviceController:getDeviceByDealer')")
    @GetMapping("/getDeviceByDealer")
    public Result getDeviceByDealer(Integer page,Integer limit){
        return deviceService.getDeviceByDealer(page, limit);
    }

    /**
     * 批量添加设备信息
     * @param file
     * @param type
     * @return
     */
    @PreAuthorize("hasAuthority('noahplat-service-device:DeviceController:addExcel')")
    @PostMapping("/addExcel")
    public Result addExcel(@RequestParam(value = "file") MultipartFile file, @NotEmpty(message = "激活码类型不能为空") String type) throws IOException {
        return deviceService.addExcel(file, type);
    }

    /**
     * 单条添加设备
     * @param device
     * @return
     */
    @PreAuthorize("hasAuthority('noahplat-service-device:DeviceController:addDevice')")
    @PostMapping("/addDevice")
    public Result addDevice(@RequestBody Device device){
        return deviceService.addDevice(device);
    }

    /**
     * 修改设备信息
     * @param device
     * @return
     */
    @PreAuthorize("hasAuthority('noahplat-service-device:DeviceController:updateDevice')")
    @PutMapping("/updateDevice")
    public Result updateDevice(@RequestBody Device device){
        return deviceService.updateDevice(device);
    }

    /**
     * 条件查询设备信息
     * @param dealer
     * @param imei
     * @param prod
     * @param page
     * @param limit
     * @return
     */
    @PreAuthorize("hasAuthority('noahplat-service-device:DeviceController:getAdminDevice')")
    @GetMapping("/getAdminDevice")
    public Result getAdminDevice(Long dealer,String imei,String prod,@RequestParam(defaultValue = "1")Integer page,@RequestParam(defaultValue = "10")Integer limit){
        return deviceService.getAdminDevice(dealer, imei, prod, page, limit);
    }

    /**
     *redis维护工地项目人员
     * @param imei
     * @return
     */
    @PreAuthorize("hasAuthority('noahplat-service-device:DeviceController:addWorkerSite')")
    @PostMapping("/addWorkerSite")
    public Result addWorkerSite(String imei){
        return deviceService.addWorkerSite(imei);
    }

}
